﻿using NetworkPower.Common;
using NetworkPower.Data.Models;
using NPOI.HSSF.UserModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NetworkPower
{
    public partial class DataList : Form
    {
        static NetworkPowerContext context = new NetworkPowerContext();
        private Dictionary<Guid, string> LineProtectBoxs = new Dictionary<Guid, string>();
        private Dictionary<Guid, string> Departments = new Dictionary<Guid, string>();
        bool canSelectLineProtectBox = false;
        bool canSelectDepartment = false;
        public DataList()
        {
            InitializeComponent();
            dataGridView1.AllowUserToAddRows = false;
            LoadData();
            InitialLineProtectBox();
        }

        private void LoadData()
        {
            context.Set<LineProtectBox>().ToList().ForEach(s => LineProtectBoxs.Add(s.LineProtectBoxId, s.LineProtectBoxName));
        }

        private void InitialLineProtectBox()
        {
            canSelectLineProtectBox = false;
            cmbLineProtectBox.DataSource = new BindingSource(LineProtectBoxs, null);
            canSelectLineProtectBox = true;
        }

        private void InitialDepartmentSource(string lineProtectBoxId)
        {
            Departments.Clear();
            context.Set<Department>().ToList().Where(s => s.LineProtectBoxId.ToString() == lineProtectBoxId).ToList().ForEach(se => Departments.Add(se.DepartmentId, se.DepartmentName));
            //LineProtec.Departments.ForEach(s => Departments.Add(s.DepartmentId, s.DepartmentName));
            canSelectDepartment = false;
            this.cmdDepartment.DataSource = new BindingSource(Departments, null);
            cmdDepartment.DisplayMember = "Value";
            cmdDepartment.ValueMember = "Key";
            canSelectDepartment = true;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (canSelectDepartment)
            {
                var para = cmdDepartment.SelectedValue;
                var datasource = context.Set<LoopLine>().ToList().Where(s => s.DepartmentId.ToString() == para.ToString()).OrderBy(o => o.RowIndex).ToList();
                dataGridView1.Rows.Clear();
                foreach (var item in datasource)
                {
                    DataGridViewCellStyle style = new DataGridViewCellStyle();
                    int rowIndex = dataGridView1.Rows.Add();
                    this.dataGridView1.Rows[rowIndex].Cells[0].Value = item.LoopLineName;
                    this.dataGridView1.Rows[rowIndex].Cells[0].Style.BackColor = Color.FromName(item.LoopLineNameColor);
                    this.dataGridView1.Rows[rowIndex].Cells[1].Value = item.InternalLine;
                    this.dataGridView1.Rows[rowIndex].Cells[2].Value = item.TerminalNo;
                    this.dataGridView1.Rows[rowIndex].Cells[2].Style.BackColor = Color.FromName(item.TerminalNoColor);
                    this.dataGridView1.Rows[rowIndex].Cells[3].Value = item.OuterLine;
                    this.dataGridView1.Rows[rowIndex].Cells[4].Value = item.LoopNo;
                }
            }
        }

        private void cmbLineProtectBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (canSelectLineProtectBox)
            {
                var selectedValue = cmbLineProtectBox.SelectedValue.ToString();
                InitialDepartmentSource(cmbLineProtectBox.SelectedValue.ToString());
            }
        }
    }
}
